package pl.kosmowska.ewelina.client.views.impl;

import java.util.List;

import pl.kosmowska.ewelina.client.ViewMessages;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.StockDataProxy;
import pl.kosmowska.ewelina.client.views.StockView;
import pl.kosmowska.ewelina.client.views.widgets.MessageDialogBox;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanDialog;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEvent;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEvent.Selected;
import pl.kosmowska.ewelina.client.views.widgets.dialogs.BooleanEventHandler;
import pl.kosmowska.ewelina.client.views.widgets.grids.StockDataGrid;
import pl.kosmowska.ewelina.client.views.widgets.popup.PopupMessage;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.ResizeLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class StockViewImpl extends Composite implements StockView{

	private static StockViewImplUiBinder uiBinder = GWT
			.create(StockViewImplUiBinder.class);

	@UiField DockLayoutPanel stockLayout;
	@UiField ResizeLayoutPanel stockPanel;
	@UiField Button addStock;
	@UiField Button editStock;
	@UiField Button deleteStock;
	private BooleanDialog wconfirm;

	interface StockViewImplUiBinder extends UiBinder<Widget, StockViewImpl> {
	}

	private Presenter presenter;
	boolean alreadyInit;


	private StockDataGrid<StockDataProxy> stockGrid;

	public StockViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		
	}

	public void reInitPagingDataGrid(){
		stockGrid = new StockDataGrid<StockDataProxy>();
		stockGrid.setHeight("600px");
		stockPanel.clear();
		stockPanel.add(stockGrid);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}


	public void reInitView(List<StockDataProxy> stockDataList) {
		System.out.println("StockViewImpl.setLoggedIn(): Loading Stock");
		reInitPagingDataGrid();
		if (stockDataList == null || stockDataList.size() == 0) {
			showMessage(ViewMessages.NO_DATA_TO_SHOW);
		}else{
			stockGrid.setDataList(stockDataList);	
		}
	}


	@UiHandler("deleteStock")
	void onDeleteStockClick(ClickEvent event) {
		if(stockGrid.getSelectedItem()!=null){
			showDeleteConfirmDialog();
		}else{
			showMessage(ViewMessages.NOTHING_SELECTED_TO_DELETE);
		}

	}
	@UiHandler("editStock")
	void onEditStockClick(ClickEvent event) {
		if(stockGrid.getSelectedItem()!=null){
			presenter.goToStockEditPlace(stockGrid.getSelectedItem());
		}else{
			showMessage(ViewMessages.NOTHING_SELECTED_TO_EDIT);
		}

	}


	@Override
	public void showMessage(String message) {
//		MessageDialogBox messageDialogBox = new MessageDialogBox(message);
//		messageDialogBox.center();
//		messageDialogBox.show();
        final PopupMessage alertMessage = new PopupMessage(message);
        alertMessage.startProcessing();
        Timer t = new Timer() {
            public void run() {
                alertMessage.stopProcessing();
            }
        };
        t.schedule(1000);
	}


	@UiHandler("addStock")
	void onAddStockClick(ClickEvent event) {
		presenter.goToStockAddPlace();
	}

	private void showDeleteConfirmDialog() {
		if (wconfirm == null) {
			wconfirm = new BooleanDialog(ViewMessages.CONFIRM_DELETE);
			wconfirm.addSelectionHandler(new BooleanEventHandler() {
				public void onBooleanEvent(BooleanEvent event) {
					if (event.getBooleanEvent() == Selected.TRUE) {
						presenter.deleteStockData(stockGrid.getSelectedItem());
					} else if (event.getBooleanEvent() == Selected.FALSE) {
						// do nothing
					}
				}
			});
		}
		wconfirm.center();
	}

	public void updateAfterDelete(StockDataProxy stockDataProxy){
		stockGrid.removeFromDataList(stockDataProxy);
	}

	@Override
	public boolean isAlreadyInit() {
		return alreadyInit;
	}

	@Override
	public void setAlreadyInit(boolean alreadyInit) {
		this.alreadyInit = alreadyInit;
	}
	
	

}


